package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.service.AdChannelService;
import com.heima.model.admin.dtos.ChannelDto;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.admin.mapper.AdChannelMapper;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.helper.StringUtil;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service("AdChannelService")
public class AdChannelServiceimpl extends ServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService {

    @Override
    public ResponseResult findByNameAndPage(ChannelDto dto) {

        //判断数据是否为kong
        if (dto == null) {
            return PageResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "无效参数");
        }
        //检查分页参数
        dto.checkParam();

        //分页模糊chaxun

        Page page = new Page(dto.getPage(), dto.getSize());

        LambdaQueryWrapper<AdChannel> wrapper = Wrappers.lambdaQuery();

        //如果频道名buweikong
        if (!StringUtil.isBlank(dto.getName())) {
            wrapper.like(AdChannel::getName, dto.getName());
        }

        //安排排列方式

        wrapper.orderByAsc(AdChannel::getOrd);
        //将条件加入分页查询
        //this.page(page,wrapper);
        IPage result = page(page, wrapper);

        PageResponseResult pageResponseResult = new PageResponseResult(dto.getPage(), dto.getSize(), result.getTotal());
        pageResponseResult.setData(result.getRecords());

        return pageResponseResult;
    }

    @Override
    public ResponseResult insert(AdChannel adChannel) {
        //判断频道是否存在
        if (adChannel == null || StringUtils.isBlank(adChannel.getName())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        LambdaQueryWrapper<AdChannel> wrapper = Wrappers.lambdaQuery();

        wrapper.eq(AdChannel::getName, adChannel.getName());

        int count = this.count(wrapper);

        if (count > 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "该频道已存在");
        }

        adChannel.setCreatedTime(new Date());

        save(adChannel);

        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult update(AdChannel adChannel) {

        if (adChannel == null || adChannel.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        AdChannel channel = getById(adChannel.getId());
        if (channel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "频道信息不存在");
        }


        if (StringUtils.isNotBlank(adChannel.getName()) && !adChannel.getName().equals(channel.getName())) {
            int count = this.count(Wrappers.<AdChannel>lambdaQuery()
                    .eq(AdChannel::getName, adChannel.getName()));
            if (count > 0) {
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "该频道已存在");
            }
        }

        updateById(adChannel);

        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult deleteById(Integer id) {


        if(id == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.判断当前频道是否存在 和 是否有效
        AdChannel adChannel = this.getById(id);
        if(adChannel==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        // 启用状态下不能删除
        if (adChannel.getStatus()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //测试统一错误
        //int i=1/0;

        //3.删除频道
       this.removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

}
